package handle

import (
	"errors"
	"strings"
)

type LibVersion struct {
	Versions  map[string]string `json:"versions" toml:"versions"`
	Libraries map[Key]Library   `json:"libraries" toml:"libraries"`
	Plugins   map[Key]Plugin    `json:"plugins" toml:"plugins"`
	Bundles   map[Key][]string  `json:"bundles" toml:"bundles"`
}

func NewLibVersion() LibVersion {
	return LibVersion{
		Versions:  make(map[string]string),
		Libraries: make(map[Key]Library),
		Plugins:   make(map[Key]Plugin),
		Bundles:   make(map[Key][]string),
	}
}

type Plugin struct {
	Id         string `json:"id" toml:"id"`
	VersionRef string `json:"version.ref" toml:"version.ref"`
}
type Library struct {
	Module     string `json:"module" toml:"module"`
	VersionRef string `json:"version.ref" toml:"version.ref"`
}

func (lib Library) Dep() Dep {
	split := strings.Split(lib.Module, ":")
	if len(split) != 2 {
		panic(errors.New("invalid dependency format"))
	}
	return Dep{
		Method:     "",
		GroupId:    split[0],
		ArtifactId: split[1],
		Version:    lib.VersionRef,
		Scope:      "",
	}
}
